package com.yunyao.web.controller;

import com.yunyao.common.dto.payment.PaymentDto;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.common.web.util.YResult;
import com.yunyao.framework.dto.audit.ApprovalDto;
import com.yunyao.framework.dto.audit.SubmitApprovalDto;
import com.yunyao.framework.dto.payment.PaymentNodeSaveDto;
import com.yunyao.framework.dto.payment.PaymentSaveDto;
import com.yunyao.framework.service.PaymentService;
import com.yunyao.framework.vo.payment.PaymentContractInvoiceVO;
import com.yunyao.framework.vo.payment.PaymentHistoryVO;
import com.yunyao.framework.vo.payment.PaymentNodeVO;
import com.yunyao.framework.vo.payment.PaymentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 付款管理
 *
 * @author chenyejian
 * @Date 2024/7/31 11:13
 */
@RestController
@RequestMapping("/payment")
@Validated
public class PaymentController {
    @Autowired
    private PaymentService paymentService;


    @GetMapping("/list")
    public YResult<PageResult<PaymentVO>> list(PaymentDto paymentDto) {
        if (!StringUtils.isEmpty(paymentDto.getBeginDate())) {
            paymentDto.setBeginDate(paymentDto.getBeginDate() + " 00:00:00");
        }
        if (!StringUtils.isEmpty(paymentDto.getEndDate())) {
            paymentDto.setEndDate(paymentDto.getEndDate() + " 23:59:59");
        }
        return paymentService.pageList(paymentDto);
    }


    @GetMapping("/info")
    public YResult<PaymentVO> info(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getId())) {
            return YResult.error("id不能为空");
        }
        return paymentService.getDetailById(paymentDto.getId());
    }


    /**
     * 删除收款信息
     */
    @PostMapping("/deleteById")
    public YResult<String> deleteById(@RequestBody PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getId())) {
            return YResult.error("id不能为空");
        }
        return paymentService.deleteById(paymentDto.getId());
    }


    /**
     * 新增、编辑
     */
    @PostMapping("/saveOrUpdate")
    public YResult<String> saveOrUpdate(@RequestBody PaymentSaveDto dto) {
        if (null == dto) {
            return YResult.error("不能为空");
        }
        return paymentService.saveOrUpdate(dto);
    }


    /**
     * 新增、编辑
     */
    @PostMapping("/node/saveOrUpdate")
    public YResult<String> saveOrUpdate(@RequestBody PaymentNodeSaveDto dto) {
        if (null == dto) {
            return YResult.error("不能为空");
        }
        return paymentService.nodeSaveOrUpdate(dto);
    }


    /**
     * 合同收款历史
     */
    @GetMapping("/history")
    public YResult<List<PaymentHistoryVO>> history(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getContractId()) && StringUtils.isEmpty(paymentDto.getReceiveId())) {
            return YResult.error("contractId或ReceiveId不能为空");
        }
        return paymentService.getHistoryList(paymentDto);
    }


    /**
     * 合同收款发票
     */
    @GetMapping("/invoice")
    public YResult<List<PaymentContractInvoiceVO>> invoice(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getContractId())) {
            return YResult.error("contractId不能为空");
        }
        return paymentService.getInvoiceList(paymentDto);
    }

    /**
     * 合同收款发票
     */
    @GetMapping("/getReceiveInvoice")
    public YResult<List<PaymentContractInvoiceVO>> getReceiveInvoice(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getReceiveId())) {
            return YResult.error("receiveId不能为空");
        }
        return paymentService.getReceiveInvoiceList(paymentDto);
    }


    /**
     * 合同发票关联收款发票
     */
    @PostMapping("/invoiceConnect")
    public YResult<String> invoiceConnect(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getInvoiceId())) {
            return YResult.error("InvoiceId不能为空");
        }
        if (StringUtils.isEmpty(paymentDto.getReceiveId())) {
            return YResult.error("ReceiveId不能为空");
        }
        return paymentService.invoiceConnect(paymentDto);
    }


    /**
     * 合同收款节点
     */
    @GetMapping("/node")
    public YResult<List<PaymentNodeVO>> node(PaymentDto paymentDto) {
        if (StringUtils.isEmpty(paymentDto.getId())) {
            return YResult.error("id不能为空");
        }
        return paymentService.getPaymentNodeList(paymentDto);
    }



    /**
     * 提交审批收款管理
     */
    @PostMapping("/submit/approval")
    public YResult<String> submitApproval(@RequestBody @Validated SubmitApprovalDto submitApprovalDto) {
        return paymentService.submitApproval(submitApprovalDto);
    }


    /**
     * 审批收款管理
     *
     * @return
     */
    @PostMapping("/approval")
    public YResult<String> approval(@RequestBody @Validated ApprovalDto approvalDto) {
        return paymentService.approval(approvalDto);
    }
}
